package com.controller.manager;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.service.RegistrationService;

@Controller
@RequestMapping("/registration")
public class RegistrationController {

	@Resource
	private RegistrationService registrationService;

	@RequestMapping(value = "/toRegistrationPage")
	public ModelAndView toRegistrationPage(HttpServletRequest request) {
		String username = (String) request.getSession().getAttribute("username");
		if (username == null || "".equals(username)) {
			return new ModelAndView("/login");
		}
		return new ModelAndView("/manager/registration");
	}

	@RequestMapping(value = "/addRegistrationInfo", method = RequestMethod.POST)
	@ResponseBody
	public String addRegistrationInfo(@RequestParam Map<String, String> paramsMap) {
		int result = registrationService.addRegistrationInfo(paramsMap);
		if (result >= 0) {
			return "success";
		}
		return "error";
	}

	// 删除
	@RequestMapping(value = "/deleteRegistrationInfo", method = RequestMethod.POST)
	@ResponseBody
	public String deleteRegistrationInfo(@RequestParam Map<String, String> paramsMap) {
		try {
			int result = registrationService.deleteRegistrationInfo(paramsMap);
			if (result >= 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	@RequestMapping(value = "/updateRegistrationInfo", method = RequestMethod.POST)
	@ResponseBody
	public String updateRegistrationInfo(@RequestParam Map<String, String> paramsMap) {
		try {
			int result = registrationService.updateRegistrationInfo(paramsMap);
			if (result > 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	@RequestMapping(value = "/getRegistrationInfo", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, String>> getRegistrationInfo(@RequestParam Map<String, String> paramsMap) {
		try {
			List<Map<String, String>> list = registrationService.getRegistrationInfo(paramsMap);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@RequestMapping(value = "/getRegistration", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView getRegistration(HttpServletRequest request, @RequestParam Map<String, String> paramsMap) {
		List<Map<String, String>> registrations = registrationService.getRegistrationById(paramsMap);
		Map<String, String> registration = registrationService.getRegistration(paramsMap);
		System.out.println(registrations);
		request.setAttribute("registrations", registrations);
		request.setAttribute("registration", registration);
		return new ModelAndView("/manager/index_five");
	}

	// 第四个页面
	@RequestMapping(value = "/getMaintenance", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView getMaintenance(HttpServletRequest request, @RequestParam Map<String, String> paramsMap) {
		List<Map<String, String>> registration = registrationService.getRegistrationByDeviceId(paramsMap);
		request.setAttribute("registration", registration);
		return new ModelAndView("/manager/index_four");
	}
}
